using Zhaoxi.Zhihu.SharedKerel.Messaging;
using Zhaoxi.Zhihu.SharedKerel.Result;
using Zhaoxi.Zhihu.UseCases.Common.Attributes;
using Zhaoxi.Zhihu.UseCases.Common.Queries.Interfaces;

namespace Zhaoxi.Zhihu.UseCases.AppUsers.Queries;

[Authorize]
public record GetUserInfoQuery(int Id) : IQuery<Result<UserInfoDto>>;

public class GetUserInfoQueryHandler (IDaraQueryService queryService): IQueryHandler<GetUserInfoQuery, Result<UserInfoDto>>
{
    public async Task<Result<UserInfoDto>> Handle(GetUserInfoQuery request, CancellationToken cancellationToken)
    {
        //构造查询器
        var queryable = queryService.AppUsers
            .Where(u => u.Id == request.Id)
            .Select(u => new UserInfoDto
            {
                Id = u.Id,
                Nickname = u.Nickname,
                Avatar = u.Avatar,
                Bio = u.Bio,
                FolloweesCount = u.Followees.Count,
                FollowersCount = u.Followers.Count
            });
        //查询
        var appUserInfo=await queryService.FirstOrDefaultAsync(queryable);
        
        return appUserInfo is null? Result.NotFound():Result.Success(appUserInfo);
    }
}